<?php

/**
 * Youtube Helper
 */
class Youtube_Helper {
	
	/**
	 * Add params to an URL
	 */
	public static function addParams($link, $opts = array()) {
		if (!empty($opts['params'])) {
			foreach ($opts['params'] as $name => $value) {
				$link .= "&".urlencode($name)."=".urlencode($value);
			}
		}
		return $link;
	}
	
	/**
	 * Transform a classic link youtube into an embed one
	 */
	public static function linkToEmbed($link, $opts = array()) {
		$params = explode("=", substr($link, strpos($link, "?")+1));
		$videoid = $params[1];
		$linkEmbed = "http://www.youtube.com/embed/".$videoid."?";
		$first = true;
		foreach ($opts as $name => $value) {
			if (!$first) {
				$linkEmbed .= "&";
			}
			$linkEmbed .= $name."=".rawurlencode($value);
			$first = false;
		}
		return $linkEmbed;
	}
}
